package locale

import (
	"encoding/json"

	"github.com/gin-gonic/gin"
	"gpt-api/framework/ginplus"
)

const (
	cnName = "zh-CN"
	hkName = "zh-HK"
	enName = "en"
	thName = "th" // 泰语
)

type Locales struct {
	CNName      string `json:"zh-CN"`
	HKName      string `json:"zh-HK"`
	ENName      string `json:"en"`
	DefaultName string `json:"default"`
}

func (l *Locales) GetName(ctx *gin.Context) string {
	return GetLocales(ctx, l)
}

func LocaleText(ctx *gin.Context, text []byte) string {
	var t Locales
	json.Unmarshal(text, &t)

	language := ginplus.Language(ctx)
	var localeName string
	switch language {
	case cnName:
		localeName = t.CNName
	case hkName:
		localeName = t.HKName
	case enName, thName:
		localeName = t.ENName
	}

	if localeName == "" {
		localeName = t.DefaultName
	}

	return localeName
}

func GetShortName(ctx *gin.Context, shortName []byte, name []byte) string {
	n := LocaleText(ctx, shortName)
	if n != "" {
		return n
	}

	return LocaleText(ctx, name)
}

func GetLocales(ctx *gin.Context, multi *Locales) string {
	lang := ginplus.Language(ctx)
	var localeName string
	switch lang {
	case cnName:
		localeName = multi.CNName
	case hkName:
		localeName = multi.HKName
	case enName:
		localeName = multi.ENName
	}

	if localeName == "" {
		localeName = multi.DefaultName
	}

	return localeName
}
